package leetcode.code1838;

import java.util.Arrays;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public int maxFrequency(int[] nums, int k) {
		Arrays.sort(nums);
		int len = nums.length;

		int[] sums = new int[len + 1];
		for (int i = 1; i <= len; i++) {
			sums[i] = sums[i - 1] + nums[i - 1];
		}
		int l = 0, r = 0, num = nums[0];
		int ans = Integer.MIN_VALUE;
		while (r < len) {
			num = nums[r];
			while (r < len && nums[r] == num) {
				r++;
			}
			int sum = (r - l) * num;
			while (sums[r] - sums[l] + k < sum) {
				l++;
				sum = (r - l) * num;
			}
			ans = Math.max(ans, r - l);
		}
		return ans;
	}

	@Override
	public void debug4() {
		H.compare(3, this.maxFrequency(H.str2array3("[1,2,4]"), 5));
		H.compare(2, this.maxFrequency(H.str2array3("[1,4,8,13]"), 5));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
